package io.github.codelifeleno.jdbrelease.execute;


import io.github.codelifeleno.jdbrelease.execute.StringStream.NegativeRule;
import io.github.codelifeleno.jdbrelease.execute.StringStream.PositiveRule;
import io.github.codelifeleno.jdbrelease.execute.StringStream.RuleBasedStreamReader;
import io.github.codelifeleno.jdbrelease.execute.StringStream.RuleCombinators;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.io.*;
import java.nio.charset.StandardCharsets;

class RulesTest {

    @Test
    void run() {
        ClassLoader classLoader = RulesTest.class.getClassLoader();

//        File file = new File("E:\\Workspace\\GitOnlineProjects\\JDBRelease\\cli\\target\\db\\changelog\\20250327\\V0001__PostgreSQL.sql");

        InputStream inputStream = ClassLoader.getSystemResourceAsStream("db/changelog/20250327/V0001__PostgreSQL.sql");
        Reader reader = null;
        Assertions.assertNotNull(inputStream);
        reader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
        BufferedReader bufferedReader = new BufferedReader(reader);

        PositiveRule p1 = (b, c) -> {
            boolean x = b.toString().endsWith(";");
            boolean y = b.toString().endsWith("//");
            if (x) {
                b.setLength(b.length() - 1);
                return true;
            } else if (y) {
                b.setLength(b.length() - 2);
                return true;
            } else {
                return false;
            }

        };
        String s = System.lineSeparator();
        NegativeRule n1 = (b, c) -> b.toString().trim().startsWith("--") && b.toString().trim().endsWith("\\\\n");
        NegativeRule n2 = (b, c) -> b.toString().trim().startsWith("#") && b.toString().trim().endsWith("\\\\n");
        NegativeRule n3 = (b, c) -> b.toString().trim().contains("/*") && b.toString().trim().endsWith("*/");
        RuleBasedStreamReader processor2 = new RuleBasedStreamReader(bufferedReader, p1, RuleCombinators.or(n1, n2, n3));
//        System.out.println(processor2.readAll());
        try {
            processor2.stream(System.out::println);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}